package com.awspaass.user.apps.exchangecenterreservation;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.server.bind.annotation.Controller;
import com.actionsoft.bpms.server.bind.annotation.Mapping;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.apps.shhtaerospaceindustrial.sms.SmsUtil;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;
/*
 * 交流中心客房预订CMD
 */
@Controller
public class AppCmdGuestRoomReservation {
	/*
	 * 交流中心销售取消客房已接单订单
	 * 0:未提交|1:已提交|2:已接单|3:已取消
	 */
	@Mapping("com.awspaass.user.apps.CateringGuestRoom_saleCancelAssign")
	public String saleGuestRoomCancelAssign(String bindid ,String reason, UserContext uc) {
		JSONObject returnData = new JSONObject();
		String GUANLIYUAN =uc.getUserModel().getUserName();//办理人名称
		String TELEPHONE =uc.getUserModel().getMobile();//节点办理人电话
		if(null == bindid || "".equals(bindid)  ) {
			returnData.put("status", "0");
			returnData.put("message", "请检查参数!");
			return returnData.toString();
		}
		try {
			String querySql = "select ROOMTYPE ,ORDERNUM,BDATE,EDATE ,APPLYNAME ,APPLYUSERCELLPHONE from BO_EU_SH_JLCENTER_RESERVATION  WHERE BINDID = " + "'"
					+ bindid + "'";
			String ROOMTYPE = CoreUtil.objToStr(DBSql.getString(querySql, "ROOMTYPE"));// 客房类型
			String ORDERNUM = CoreUtil.objToStr(DBSql.getString(querySql, "ORDERNUM"));// 客房数量
			String BDATE = CoreUtil.objToStr(DBSql.getString(querySql, "BDATE"));// 抵店日期
			String EDATE = CoreUtil.objToStr(DBSql.getString(querySql, "EDATE"));// 离店日期
			String APPLYNAME = CoreUtil.objToStr(DBSql.getString(querySql, "APPLYNAME"));// 联系人
			String APPLYUSERCELLPHONE = CoreUtil.objToStr(DBSql.getString(querySql, "APPLYUSERCELLPHONE"));// 联系电话
			
			String message = "{'APPLYNAME':'" + APPLYNAME + "','BDATE':'" + BDATE + "','EDATE':'" + EDATE
					+ "','ROOMTYPE':'" + ROOMTYPE + "','ORDERNUM':'" + ORDERNUM+ "','GUANLIYUAN':'" + GUANLIYUAN+ "','TELEPHONE':'" + TELEPHONE+"'}";
			String updateAssignSql = "update BO_EU_SH_JLCENTER_RESERVATION set ORDERSTATUS = '3',FORCEDCANCEL = '"+reason+"' where BINDID = '" + bindid + "'";
			DBSql.update(updateAssignSql);
			SmsUtil.sendSms(APPLYUSERCELLPHONE, "SMS_249300776", message);
			returnData.put("status", "0");
			returnData.put("message", "取消成功！");
		} catch (Exception e) {
			returnData.put("status", "1");
			returnData.put("message", e.getMessage());
			e.printStackTrace();

		}
		return returnData.toString();

	}
	/*
	 * 加载用户客房预订完成的交流中心销售已接单订单的所有订单
	 */
	@Mapping("com.awspaass.user.apps.CateringGuestRoom_saleDetails")
	public static String getCateringGuestRoom_saleDetails(UserContext uc,String rzbegin,String rzend,String ydperson) {
		JSONObject result = new JSONObject();
		String checksql="";
		String sql="";
		if(!"".equals(rzbegin)) {//抵店时间大于等于
			sql = " and t.BDATE >='" + rzbegin + "' ";
			checksql +=sql;
		}
		if(!"".equals(rzend)) { //抵店时间小于等于
			sql =" and t.BDATE <='" + rzend + "' ";
			checksql +=sql;
		}
		if(!"".equals(ydperson)) { //预订人
			sql =" and t.APPLYNAME like '%" + ydperson + "%' ";
			checksql +=sql;
		}
		try {
			if(!"".equals(checksql)) {
				String querySql = "select t.APPLYNAME ,t.ROOMTYPE,t.BDATE,t.EDATE,t.ORDERSTATUS,t.BINDID from  BO_EU_SH_JLCENTER_RESERVATION  t  where t.ORDERSTATUS in ('2') "
						+ checksql + " order by  t.BDATE  asc ";
				List<Map<String, Object>> urlList = DBSql.query(querySql, new ColumnMapRowMapper(), new Object[] {});
				if (urlList == null || urlList.isEmpty()) {
					JSONArray itemArray = new JSONArray();
					result.put("status", "0");
					result.put("item", itemArray);
				}
				JSONArray itemArray = new JSONArray();
				for (int i = 0; i < urlList.size(); i++) {
					Map<String, Object> urlMap = urlList.get(i);
					JSONObject item = new JSONObject();
					String APPLYNAME = CoreUtil.objToStr(urlMap.get("APPLYNAME"));// 预定人
					String ROOMTYPE = CoreUtil.objToStr(urlMap.get("ROOMTYPE"));// 客房类型
					String BDATE = CoreUtil.objToStr(urlMap.get("BDATE"));// 抵店时间
					String EDATE = CoreUtil.objToStr(urlMap.get("EDATE"));// 离店时间
					String ORDERSTATUS = CoreUtil.objToStr(urlMap.get("ORDERSTATUS"));// 订单状态
					String BINDID = CoreUtil.objToStr(urlMap.get("BINDID"));// 流程实例id
					item.put("applyname", APPLYNAME);	
					item.put("roomtype", ROOMTYPE);
					item.put("bdate", BDATE);
					item.put("edate", EDATE);
					item.put("bindid", BINDID);
					item.put("orderstatus", ORDERSTATUS);
					itemArray.add(item);
					
				}
				result.put("status", "0");
				result.put("item", itemArray);
			}else {
			Date now = new Date();// 系统日期
			SimpleDateFormat DateFormat = new SimpleDateFormat("yyyy-MM-dd");
			String today = DateFormat.format(now);// 日期
			Calendar calendar = Calendar.getInstance();  
		    calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + 6);  
		    Date endtoday = calendar.getTime();  
		    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
		    String enddate = format.format(endtoday);  
			String querySql = "select t.APPLYNAME ,t.ROOMTYPE,t.BDATE,t.EDATE,t.ORDERSTATUS,t.BINDID from  BO_EU_SH_JLCENTER_RESERVATION  t  where t.ORDERSTATUS in ('2') "
					+ " and  t.BDATE >=  '"+ today +"' and  t.BDATE <=  '"+ enddate + "' order by  t.BDATE  asc ";
			List<Map<String, Object>> urlList = DBSql.query(querySql, new ColumnMapRowMapper(), new Object[] {});
			if (urlList == null || urlList.isEmpty()) {
				JSONArray itemArray = new JSONArray();
				result.put("status", "0");
				result.put("item", itemArray);
			}
			JSONArray itemArray = new JSONArray();
			for (int i = 0; i < urlList.size(); i++) {
				Map<String, Object> urlMap = urlList.get(i);
				JSONObject item = new JSONObject();
				String APPLYNAME = CoreUtil.objToStr(urlMap.get("APPLYNAME"));// 预定人
				String ROOMTYPE = CoreUtil.objToStr(urlMap.get("ROOMTYPE"));// 客房类型
				String BDATE = CoreUtil.objToStr(urlMap.get("BDATE"));// 抵店时间
				String EDATE = CoreUtil.objToStr(urlMap.get("EDATE"));// 离店时间
				String ORDERSTATUS = CoreUtil.objToStr(urlMap.get("ORDERSTATUS"));// 订单状态
				String BINDID = CoreUtil.objToStr(urlMap.get("BINDID"));// 流程实例id
				item.put("applyname", APPLYNAME);
				item.put("roomtype", ROOMTYPE);
				item.put("bdate", BDATE);
				item.put("edate", EDATE);
				item.put("bindid", BINDID);
				item.put("orderstatus", ORDERSTATUS);
				itemArray.add(item);
			}
			result.put("status", "0");
			result.put("item", itemArray);
		  }
		} catch (Exception e) {
			result.put("status", "1");
			result.put("message", e.getMessage());
			e.printStackTrace();
		}

		return result.toString();
	}
	/*
	 * 用户取消客房预订订单  
	 */
	@Mapping("com.awspaass.user.apps.CateringGuestRoom_userCancelAssign")
	public String userGuestRoomCancelAssign(String bindid,String reason, UserContext uc) {
		JSONObject returnData = new JSONObject();
		String GUANLIYUAN =uc.getUserModel().getUserName();//办理人名称
		String TELEPHONE =uc.getUserModel().getMobile();//节点办理人电话
		if(null == bindid || "".equals(bindid)  ) {
			returnData.put("status", "0");
			returnData.put("message", "请检查参数!");
			return returnData.toString();
		}
		
		try {
			String querySql = "select ROOMTYPE ,ORDERNUM,BDATE,EDATE ,APPLYNAME ,APPLYUSERCELLPHONE,ORDERSTATUS "
					+ ",APPLYUID,XSCOMPANY from BO_EU_SH_JLCENTER_RESERVATION  WHERE BINDID = '" + bindid + "'";
			String ROOMTYPE = CoreUtil.objToStr(DBSql.getString(querySql, "ROOMTYPE"));// 客房类型
			String ORDERNUM = CoreUtil.objToStr(DBSql.getString(querySql, "ORDERNUM"));// 客房数量
			String BDATE = CoreUtil.objToStr(DBSql.getString(querySql, "BDATE"));// 抵店日期
			String EDATE = CoreUtil.objToStr(DBSql.getString(querySql, "EDATE"));// 离店日期
			String APPLYNAME = CoreUtil.objToStr(DBSql.getString(querySql, "APPLYNAME"));// 联系人
			String APPLYUSERCELLPHONE = CoreUtil.objToStr(DBSql.getString(querySql, "APPLYUSERCELLPHONE"));// 联系电话
			String ORDERSTATUS = CoreUtil.objToStr(DBSql.getString(querySql, "ORDERSTATUS"));// 订单状态
			String APPLYUID = CoreUtil.objToStr(DBSql.getString(querySql, "APPLYUID"));// 订单人ID
			String XSCOMPANY = CoreUtil.objToStr(DBSql.getString(querySql, "XSCOMPANY"));// 预订单位
			String stquerySql="select  t.applyusername, t.telphone from BO_EU_SH_XSFZDW t  where t.applycompanyname = '"+XSCOMPANY+"'";
			String applyusername = CoreUtil.objToStr(DBSql.getString(stquerySql, "applyusername"));//销售管理人员姓名
			String[] name =applyusername.split(" ");
			String guanliyuan="";
			if(!"".equals(name[0])) {
				guanliyuan=name[0];
			}else {
				guanliyuan="管理员";
			}
			String telphone = CoreUtil.objToStr(DBSql.getString(stquerySql, "telphone"));//销售管理人员电话号
			
			String message = "{'APPLYNAME':'" + APPLYNAME + "','BDATE':'" + BDATE + "','EDATE':'" + EDATE
					+ "','ROOMTYPE':'" + ROOMTYPE + "','ORDERNUM':'" + ORDERNUM + "'}";
			String messageguanliyuan = "{'GUANLIYUAN':'" + guanliyuan + "','XSCOMPANY':'" + XSCOMPANY + "','APPLYNAME':'" + APPLYNAME
					+ "','BDATE':'" + BDATE + "','EDATE':'" + EDATE+ "','ROOMTYPE':'" + ROOMTYPE + "','ORDERNUM':'" + ORDERNUM+ "'}";
			String updateAssignSql = "update BO_EU_SH_JLCENTER_RESERVATION set ORDERSTATUS = '3' , FORCEDCANCEL = '"+reason+"'  where BINDID = '" + bindid + "'";
			DBSql.update(updateAssignSql);
			if("1".equals(ORDERSTATUS)) {
				try {
					System.out.println("准备终止流程！流程号:" + bindid + "用户ID:" + APPLYUID);
					SDK.getProcessAPI().terminateById(bindid, APPLYUID);
				} catch (Exception e) {
					e.printStackTrace();
				}
				SmsUtil.sendSms(APPLYUSERCELLPHONE, "SMS_249225869", message);//取消成功给用户发消息
				
			}else {
				SmsUtil.sendSms(APPLYUSERCELLPHONE, "SMS_249225869", message);//取消成功给用户发消息
				String[] telephonenum =telphone.split("、");
				if(telephonenum.length > 0) {
					for (int i = 0; i < telephonenum.length; i++) {
					SmsUtil.sendSms(telephonenum[i], "SMS_249085910", messageguanliyuan);//取消成功给调度发消息
					}
			}
				
			}
			returnData.put("status", "0");
			returnData.put("message", "取消成功！");
		} catch (Exception e) {
			returnData.put("status", "1");
			returnData.put("message", e.getMessage());
			e.printStackTrace();
		}

		return returnData.toString();

	}

	/*
	 * 加载用户预订完成的可取消的所有订单
	 */
	@Mapping("com.awspaass.user.apps.CateringGuestRoom_userDetails")
	public static String getCateringGuestRoom_userDetails(UserContext uc) {
		String userId = uc.getUID();
		JSONObject result = new JSONObject();
		try {
			Date now = new Date();// 系统日期
			SimpleDateFormat DateFormat = new SimpleDateFormat("yyyy-MM-dd");
			String today = DateFormat.format(now);// 日期
			String querySql = "select t.ROOMTYPE,t.BDATE,t.EDATE,t.ORDERSTATUS,t.BINDID from  BO_EU_SH_JLCENTER_RESERVATION  t  where t.ORDERSTATUS in ('1','2') "
					+ " and  t.BDATE >=  '"+ today +"'  and t.APPLYUID ='" + userId + "' order by  t.BDATE  asc ";
			List<Map<String, Object>> urlList = DBSql.query(querySql, new ColumnMapRowMapper(), new Object[] {});
			if (urlList == null || urlList.isEmpty()) {
				JSONArray itemArray = new JSONArray();
				result.put("status", "0");
				result.put("item", itemArray);
			}
			JSONArray itemArray = new JSONArray();
			for (int i = 0; i < urlList.size(); i++) {
				Map<String, Object> urlMap = urlList.get(i);
				JSONObject item = new JSONObject();
				String ROOMTYPE = CoreUtil.objToStr(urlMap.get("ROOMTYPE"));// 客房类型
				String BDATE = CoreUtil.objToStr(urlMap.get("BDATE"));// 抵店时间
				String EDATE = CoreUtil.objToStr(urlMap.get("EDATE"));// 离店时间
				String ORDERSTATUS = CoreUtil.objToStr(urlMap.get("ORDERSTATUS"));// 订单状态
				String BINDID = CoreUtil.objToStr(urlMap.get("BINDID"));// 流程实例id
				Date ruzhuday = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(BDATE+ " 18:00:00");//格式转换
				if ( ruzhuday.getTime()- now.getTime() >= 0 ) {
					item.put("roomtype", ROOMTYPE);
					item.put("bdate", BDATE);
					item.put("edate", EDATE);
					item.put("bindid", BINDID);
					item.put("orderstatus", ORDERSTATUS);
					itemArray.add(item);
				} 

			}
			result.put("status", "0");
			result.put("item", itemArray);
		} catch (Exception e) {
			result.put("status", "1");
			result.put("message", e.getMessage());
			e.printStackTrace();
		}

		return result.toString();
	}
}
